package com.ydj.framework.utils.generator;

import cn.hutool.core.io.FileUtil;
import com.ydj.framework.exception.CommonException;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;

/**
 * 数据库实体类生成
 */
class EntityGenerator {

    private final static String CLASS_NAME_FLAG = "${ClassName}";
    private final static String CHINESE_CLASS_NAME_FLAG = "${ChineseClassName}";
    private final static String SNACK_CLASS_NAME_FLAG = "${SnakeCaseClassName}";


    public static void generator(File projectDir, File templateDir, List<String> classNameArray, List<String> chineseClassNameArray) throws IOException, CommonException {
        // 找到输出的目录
        File outputBaseDir = new File(projectDir, "app/src/main/java/com/ydj/app/entity");
        System.out.println("输出路径是" + outputBaseDir.getAbsolutePath());

        // 生成代码了
        List<GeneratorOutputInfo> outputInfoList = new LinkedList<>();
        String javaCode;
        for (int i = 0; i< classNameArray.size(); i++) {
            String className = classNameArray.get(i);
            String chineseClassName = chineseClassNameArray.get(i);
            // entity
            javaCode = FileUtil.readString(new File(templateDir, "Entity.template"), StandardCharsets.UTF_8);
            javaCode = javaCode.replace(CLASS_NAME_FLAG, className);
            javaCode = javaCode.replace(CHINESE_CLASS_NAME_FLAG, chineseClassName);
            javaCode = javaCode.replace(SNACK_CLASS_NAME_FLAG, GeneratorUtils.toSnakeCase(className));
            outputInfoList.add(new GeneratorOutputInfo(outputBaseDir, null, className, javaCode));

            // daoImpl
            javaCode = FileUtil.readString(new File(templateDir, "Dao.template"), StandardCharsets.UTF_8);
            javaCode = javaCode.replace(CLASS_NAME_FLAG, className);
            outputInfoList.add(new GeneratorOutputInfo(outputBaseDir, "dao", className + "Dao", javaCode));

            // dao
            javaCode = FileUtil.readString(new File(templateDir, "DaoImpl.template"), StandardCharsets.UTF_8);
            javaCode = javaCode.replace(CLASS_NAME_FLAG, className);
            outputInfoList.add(new GeneratorOutputInfo(outputBaseDir, "dao/impl", className + "DaoImpl", javaCode));

            // mapper
            javaCode = FileUtil.readString(new File(templateDir, "Mapper.template"), StandardCharsets.UTF_8);
            javaCode = javaCode.replace(CLASS_NAME_FLAG, className);
            outputInfoList.add(new GeneratorOutputInfo(outputBaseDir, "mapper", className + "Mapper", javaCode));

            // query
            javaCode = FileUtil.readString(new File(templateDir, "Query.template"), StandardCharsets.UTF_8);
            javaCode = javaCode.replace(CLASS_NAME_FLAG, className);
            outputInfoList.add(new GeneratorOutputInfo(outputBaseDir, "query", className + "Query", javaCode));
        }

        // 真正生成文件
        for (GeneratorOutputInfo info : outputInfoList) {
            if (!info.getFile().exists()) {
                FileUtil.writeString(info.getJavaCode(), info.getFile(), StandardCharsets.UTF_8);
                GeneratorUtils.log("输出文件成功。路径为" + info.getFile().getAbsolutePath());
            }
        }
        GeneratorUtils.log("输出完毕, Good Luck!");
    }

}